﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
namespace App
{
    public partial class EventsList : System.Web.UI.Page
    {
        bool prerender = true;
        int state;
        int teamFromAdminWiev=-1;

        protected void Page_PreRender(object sender, EventArgs e)
        {

            if (prerender)
            {
                if (MidApp.ManageIsAdministrator.IsAdministrator(Common.GetLoggedUser(this.Page).UserID))
                {
                    int.TryParse(Server.HtmlEncode(Request.Cookies["EventAdmin"].Value),out teamFromAdminWiev);
                    if (teamFromAdminWiev == -1)//-1 takova hodnota tymu neni, proto zvoleno pro vypsani vsech udalosti adminem
                    {
                        rptEvents.DataSource = MidApp.ManageEvent.GetEventsByAuthor(Common.GetLoggedUser(this.Page).Nick);
                        rptEvents.DataBind();
                        rptEvents.Controls[0].Controls[0].FindControl("PanelFilter").Visible = false;
                    }
                    else
                    {
                        rptEvents.DataSource = MidApp.ManageEvent.GetEventsByAuthorForTeam(Common.GetLoggedUser(this.Page).Nick,teamFromAdminWiev);
                        rptEvents.DataBind();
                        rptEvents.Controls[0].Controls[0].FindControl("PanelFilter").Visible = false;
                    }
                }
                else
                {
                    SwitchDataContent(state);
                }
            }
                        
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }

            //cookie ses pri kliku na vypis akci nastavujje na hodnotu 1 (aktualni akce) uz pri kliku na vypis akce na master page
            int.TryParse(Server.HtmlEncode(Request.Cookies["State"].Value), out state);
        }

        protected string NumberOfParticipants(object sender)
        {
            var a = ((AppLib.Event)sender).EventID;

            return (MidApp.ManageParticipate.GetParticipantsOnEvent(a)).Count.ToString();
        }

        protected string Participants(object sender)
        {
            int akce = ((AppLib.Event)sender).EventID;

            var a = MidApp.ManageParticipate.GetParticipantsOnEvent(akce);

            string b = "";


            foreach (var item in a)
            {

                b = b + item.User.Nick.ToString() + " ";
            }

            return b;
        }

        //metoda zjistuje zda zobrazit tlacitko pro prihlaseni, odhlaseni
        protected bool CheckIn(object sender)
        {
            int jmeno_akce = ((AppLib.Event)sender).EventID;
            AppLib.User user=Common.GetLoggedUser(this.Page);

            return MidApp.ManageParticipate.IsUserCheckedInToEvent(((AppLib.Event)sender).EventID,Common.GetLoggedUser(this.Page).UserID);
            
        }
        protected void IsEmptyRepeator()
        {
            ICollection list = rptEvents.DataSource as ICollection;

            bool isEmpty = list.Count == 0 ? true : false;

            if (isEmpty)
            {
                lblEmptyRepeaterText.Visible = true;
            }
            else
            {
                lblEmptyRepeaterText.Visible = false;
            }
        }
        //zda jso uvsechna msita na akci jiz obsazena
        protected bool IsOccupied(object sender)
        {
            bool sign=false;
            var aux = ((AppLib.Event)sender);
            var count = (MidApp.ManageParticipate.GetParticipantsOnEvent(aux.EventID)).Count;

            if (count <aux.Seats)
                sign = true;

            return sign;

        }
        //metoda vraci kolekci hracu prihlasenych na akci, napojeno jako data source vnoreneho repeatoru
        protected List<AppLib.Participate> DataSource(object sender)
        {
            var aux = (AppLib.Event)sender;

            return MidApp.ManageParticipate.GetParticipantsOnEvent(aux.EventID);
        }
        //metoda vraci nick prihlaseneho hrace na akci
        protected string GetUserNick(object sender)
        {
            var aux = (AppLib.Participate)sender;

            return aux.User.Nick;
        }
        //metoda vraci jmeno a prijmeni
        protected string GetUserNameSurname(object sender)
        {
            var aux = (AppLib.Participate)sender;

            string name = aux.User.Name + " " + aux.User.Surname;

            return name;
        }
        //metoda vraci string hodnotu ID uzivatele
        protected string GetUserID(object sender)
        {
            var aux =(AppLib.Participate)sender;
            
            return aux.UserID.ToString();
        }
        protected string GetTeamAvatar(object sender)
        {
            var aux = (AppLib.Event)sender;
            var teamID = MidApp.ManagePossession.GetTeamInChargeOfEvent(aux.EventID).TeamID;

            return MidApp.ManageTeam.GetTeamByID(teamID).ImageURL;

        }
        protected string GetTeamID(object sender)
        {
            var aux = (AppLib.Event)sender;
            var teamID = MidApp.ManagePossession.GetTeamInChargeOfEvent(aux.EventID).TeamID;

            return teamID.ToString();
        }
        protected string GetTeamName(object sender)
        {
            var aux = (AppLib.Event)sender;
            var teamID = MidApp.ManagePossession.GetTeamInChargeOfEvent(aux.EventID).TeamID;

            return MidApp.ManageTeam.GetTeamByID(teamID).TeamName;
        }
        //povoleni, zakazani prihlaseni se na akci (cili na akce minule se nelze prihlasit)
        protected bool IsAbleToSign(object sender)
        {

            var aux = ((AppLib.Event)sender).DateOfHolding;

            if (DateTime.Compare(DateTime.Now.Date, aux.Date) <= 0)
                return true;
            else
                return false;
        }

        //metoda vypise do labelu, zda se jedna o jednorazovou nebo opakovanou akci
        protected string ActionWithSpan(object sender)
        {
            bool? repeatable = ((AppLib.Event)sender).Repeatable;
            string s="";

            if (repeatable==true)
                s = "Opakovaná";
            else 
                s = "Jednorázová";

            return s;

        }

        //Metoda vraci string jmeno barvy, podle stari data konani
        protected string EventActuality(object sender)
        {
            var aux = ((AppLib.Event)sender).DateOfHolding;

            if(DateTime.Compare(DateTime.Now.Date,aux.Date)<=0)
                return "Maroon";
            else
                return "SlateGrey";
        }

        //pro prihlaseni na udalost a vypsani hlasky
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            prerender = false;
            var button = (LinkButton)sender;
            var eventID=int.Parse(((LinkButton)sender).CommandArgument);

            AppLib.Detail newDetail = new AppLib.Detail();
            newDetail.Amount = "-" + MidApp.ManageEvent.GetEventByID(eventID).Price.ToString();
            newDetail.Detail1 = "Strhnuto za přihlášení na akci " + MidApp.ManageEvent.GetEventByID(eventID).EventName;
            newDetail.Date = DateTime.Today;
            MidApp.ManageParticipate.SignUserToEvent(eventID, Common.GetLoggedUser(this.Page).UserID, MidApp.ManagePossession.GetTeamInChargeOfEvent(eventID).TeamID, newDetail);

            Response.Redirect("EventsList.aspx");    
        }

        //odhlaseni z udalosti
        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            prerender = false;
            var button = (LinkButton)sender;
            var eventID = int.Parse(((LinkButton)sender).CommandArgument);

            AppLib.Detail newDetail = new AppLib.Detail();
            newDetail.Amount = MidApp.ManageEvent.GetEventByID(eventID).Price.ToString();
            newDetail.Detail1 = "Připsáno za odhlášení z akce " + MidApp.ManageEvent.GetEventByID(eventID).EventName;
            newDetail.Date = DateTime.Today;
            MidApp.ManageParticipate.ResignUserFromEvent(eventID, (Common.GetLoggedUser(this.Page)).UserID, MidApp.ManagePossession.GetTeamInChargeOfEvent(eventID).TeamID, newDetail);

            Response.Redirect("EventsList.aspx");
            
        }
        
        protected void LinkButtonPastEvents_Click(object sender, EventArgs e)
        {
            Response.Cookies["State"].Value = ((LinkButton)sender).CommandArgument;
            prerender = false;
            PastActions();
        }
        
        //donuti repeator vypsat akce, ktere jiz probehly
        private void PastActions()
        {
            
            rptEvents.DataSource = MidApp.ManageEvent.GetPastEventsOrderedDescending(Common.GetLoggedUser(this.Page).UserID);
            rptEvents.DataBind();
            IsEmptyRepeator();
        }
   
        protected void LinkButtonActualEvents_Click(object sender, EventArgs e)
        {
            Response.Cookies["State"].Value = ((LinkButton)sender).CommandArgument;
            prerender = false;
            ActualActions();
        }
        //donuti repeator vypsat aktualni akce
        private void ActualActions()
        {

            rptEvents.DataSource = MidApp.ManageEvent.GetActualEventsOrderedAscending(Common.GetLoggedUser(this.Page).UserID);
            rptEvents.DataBind();
            IsEmptyRepeator();
        }
        
        protected void LinkButtonAllEvents_Click(object sender, EventArgs e) 
        {
            Response.Cookies["State"].Value = ((LinkButton)sender).CommandArgument;
            prerender = false;
            Allactions();
        }
        //donuti repeator vypsat vsechny akce
        private void Allactions()
        {

            rptEvents.DataSource = MidApp.ManageEvent.GetAllEventsOrderedDescending(Common.GetLoggedUser(this.Page).UserID);
            rptEvents.DataBind();
            IsEmptyRepeator();
        }     

        protected void LinkButtonMyAction_Click(object sender, EventArgs e)
        {
            Response.Cookies["State"].Value = ((LinkButton)sender).CommandArgument;
            prerender = false;
            MyActions();
        }
        //vypise akce, na ktere je prihlasen uzivatel
        private void MyActions()
        {
            
            rptEvents.DataSource = MidApp.ManageEvent.GetHisEventsOrderedDescending(Common.GetLoggedUser(this).UserID);
            rptEvents.DataBind();
            IsEmptyRepeator();
        }

        protected void SwitchDataContent(int aux)
        {
            switch (aux)
            {
                case 1:
                    ActualActions();
                    break;
                case 2:
                    PastActions();
                    break;
                case 3:
                    Allactions();
                    break;
                case 4:
                    MyActions();
                    break;
                default:
                    ActualActions();
                    break;
            }
        }
        
        //obsluha tlacitka pro zobrazeni detailu hrace
        protected void LinkButtonUserInfo_Click(object sender, EventArgs e)
        {
            Response.Cookies["UserDetail"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("UserDetail.aspx");
        }
        protected void LinkButtonEditEvent_Click(object sender, EventArgs e)
        {
            Response.Cookies["EventIDtoEdit"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("EditEvent.aspx");
        }
        protected void LinkButtonAddPlayers_Click(object sender, EventArgs e)
        {
            Response.Cookies["AddPlay"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("AddPlayers.aspx");
        }
        protected bool IsEventLeader(object sender)
        {
            var aux = (AppLib.Event)sender;

            if (MidApp.ManageIsMember.IsTeamLeader(MidApp.ManagePossession.GetTeamInChargeOfEvent(aux.EventID).TeamID,Common.GetLoggedUser(this.Page).UserID))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        protected bool isNotAfterDate(object sender)
        {
            return IsAbleToSign(sender);

        }
        protected bool IsCreatedByAdmin(object sender)
        {
            var aux = sender as AppLib.Event;
            bool boo = false;

            if (aux.CreatedBy == "admin")
                boo = true;
            else
                boo = false;
            
            return boo;
        }
        protected bool IsAdmin(object sender)
        {
            return (MidApp.ManageIsAdministrator.IsAdministrator(Common.GetLoggedUser(this.Page).UserID));
        }
        protected bool IsAllowed()
        {
            var id=Common.GetLoggedUser(this.Page).UserID;
            bool boo = false;
            if (!MidApp.ManageIsAdministrator.IsAdministrator(id))
                boo = true;
            else
                boo = false;

            return boo;
        }

    }
}